package com.learn.springbootnetty.字节流和字符流;

import java.io.*;

/**
 * @author xiaodonghong
 * @description TODO
 * @date 2023/6/19-10:05
 */
public class FlushDemo {
    public static void main(String[] args) throws IOException, InterruptedException {
        FlushDemo.getFileOutputStream();
        FlushDemo.getBufferedOutputStream();
    }
    public static void  getFileOutputStream() throws IOException, InterruptedException {
        FileOutputStream fos = new FileOutputStream("D:\\a.txt");
        int count = 0;
        for (int i = 0; i < 32; i++) {
            for (int j = 0; j < 1024; j++) { // 每次写 1k 数据
                byte[] bytes = "a".getBytes();
                fos.write(bytes);
                count += bytes.length;
            }
            System.out.println("===" + count + "===");
            Thread.sleep(2000);
        }
        fos.close();
    }

    public static void  getBufferedOutputStream() throws IOException, InterruptedException {
        FileOutputStream fos = new FileOutputStream("D:\\b.txt");
        BufferedOutputStream buffer = new BufferedOutputStream(fos);
        int count = 0;
        for (int i = 0; i < 32; i++) {
            for (int j = 0; j < 1024; j++) {
                byte[] bytes = "a".getBytes();
                buffer.write(bytes);
                count += bytes.length;
            }
            // buffer.flush(); // 手动调用一次同步，否则会等到 Java 默认缓冲区有 8k 数据后才写入
            System.out.println("===" + count + "===");
            Thread.sleep(2000);
        }
        buffer.close();

    }

}
